<?php

namespace app\admin\controller\purchase;

use app\common\controller\Backend;
use app\common\model\Storehouse;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Goods extends Backend
{
    
    /**
     * Goods模型对象
     * @var \app\common\model\purchase\Goods
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\purchase\Goods;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            $where = [];
            if(session('admin')['storehouse_id'] >0){
                $where['pg.storehouse_id'] =  session('admin')['storehouse_id'];
            }
            $filter = $this->request->get("filter");
            $filter = (array)json_decode($filter, true);
            if(!empty($filter)){

                foreach ($filter as $key =>$value){
                    if($key == 'id'){
                        $where['pg.id'] = $value;
                    }
                    if($key == 'title'){
                        $where['g.title'] = array('like','%'.$value.'%');
                    }
                    if($key == 'operator'){
                        $where['pg.operator'] = array('like','%'.$value.'%');
                    }
                    if($key == 'name'){
                        $where2['name'] = $value;
                        $info = Storehouse::where($where2)->find();
                        $where['pg.storehouse_id'] = $info['id'];
                    }
                    if($key == 'specs'){
                        $where['g.specs'] = array('like','%'.$value.'%');
                    }
                    if($key == 'place_origin'){
                        $where['g.place_origin'] = array('like','%'.$value.'%');
                    }
                    if($key == 'group_name'){
                        $where['pg.group_name'] = array('like','%'.$value.'%');
                    }
                    if($key == 'company'){
                        $where['pg.company'] = array('like','%'.$value.'%');
                    }
                    if($key == 'status'){
                        $where['pg.status'] = $value;
                    }
                    if($key == 'nickname'){
                        $alist = Db::name('admin')->where(array('nickname'=>array('like','%'.$value.'%')))->select();
                        $ids = [];
                        foreach ($alist as $v){
                            $ids[] = $v['id'];
                        }
                        if(empty($ids)){
                            return  array("total" => 0, "rows" => []);
                        }
                        $where['a.id'] = array('in',$ids);
                    }
                    if($key == 'snick'){
                        $alist = Db::name('supplier')->where(array('nickname'=>array('like','%'.$value.'%')))->select();
                        $ids = [];
                        foreach ($alist as $v){
                            $ids[] = $v['id'];
                        }
                        if(empty($ids)){
                            return  array("total" => 0, "rows" => []);
                        }
                        $where['g.supplier_id'] = array('in',$ids);
                    }
                    if($key == 'needtime'){
                        $arr = explode(' - ', $value);
                        $where['needtime'] = array('between time',$arr);
                    }
                    if($key == 'createtime'){
                        $arr = explode(' - ', $value);
                        $where['createtime'] = array('between time',$arr);
                    }
                }
            }

            $sort = input('sort');
            $order = input('order');
            $offset = input('offset');
            $limit = input('limit');
            $list = Db::name('purchase_goods')
                ->alias('pg')
                ->where($where)
                ->field('pg.id,place_origin,specs,title,goods_num,operator,company,g.number,needtime,unit,pg.createtime,s.nickname as snick,pg.status,h.name')
                ->join('zax_goods g','pg.goods_id = g.id')
//                ->join('admin a','pg.admin_id = a.id')
                ->join('supplier s','g.supplier_id = s.id','LEFT')
                ->join('storehouse h','pg.storehouse_id = h.id')
                ->limit($offset,$limit)->order('pg.id',$order)
                ->select();

            $goods_status = $this->model->goods_status();
            foreach ($list as $k=>$v){
                $list[$k]['ystatus'] = $v['status'];
                $list[$k]['status'] = $goods_status[$v['status']];
            }

            $total = Db::name('purchase_goods')
                ->alias('pg')
                ->where($where)
                ->field('pg.id,place_origin,specs,title,goods_num,operator,company,g.number,needtime,unit,pg.createtime,s.nickname as snick,pg.status,h.name')
                ->join('zax_goods g','pg.goods_id = g.id')
//                ->join('admin a','pg.admin_id = a.id')
                ->join('supplier s','g.supplier_id = s.id','LEFT')
                ->join('storehouse h','pg.storehouse_id = h.id')
                ->count();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    //检测商品数据是否是同一供货商
    public function check(){
        $ids = json_decode(input('ids'),true);
        if(empty($ids)){
            $this->error('数据为空');
        }
        $list = Db::name('purchase_goods')
            ->alias('pg')
            ->where(array('pg.id'=>array('in',$ids)))
            ->field('g.supplier_id,pg.id,pg.status,pg.storehouse_id')
            ->join('zax_goods g','pg.goods_id = g.id')
            ->select();
//        dump($list);
//        exit;
        $sid = '';
        $hid = '';
        foreach ($list as $k=> $v){
            if($k ==0){
                $sid = $v['supplier_id'];
                $hid = $v['storehouse_id'];
            }else{
                if($v['supplier_id'] != $sid){
                    $this->error('数据存在不同供应商,请排查');
                }
                if($v['storehouse_id'] != $hid){
                    $this->error('数据采购仓库不一致,请排查');
                }
            }
            if($v['status'] !=1){
                $this->error('数据'.$v['id'].'已处理,请检查');
            }
        }
        $this->success('检测通过,正常生成采购单','',array('ids'=>implode(',',$ids),'supplier_id'=>$list[0]['supplier_id'],'storehouse_id'=>$hid));
    }
    public function del($ids = null){
        $res = $this->model->save(array('status'=>5),array('id'=>$ids));
        if($res){
            $this->success('中止成功');
        }
        $this->error('操作失败');
    }
//    public function goodsconfig(){
//        $type = input('type/d',0);
//        switch ($type){
//            case 1:
//
//        }
//    }
}
